<?php
	include_once('ContentBase.php');

	class PageServer extends ContentBase
	{
		var $Page = 		array (
			'pid'	=> "",
			'title'	=> "",
			'ministry_id'   	=> "",  //ministry, department, sections of organization to which this individual belongs
			'content' => "",
			'error'  		=> "");

		var $PageGroup = 		array (
			'pages'	=> array(), //to hold an array of pages array(PID => Title) for a particular div_code
			'ministry_id'   	=> "",  //ministry, department, sections of organization to which this individual belongs
			'error'  		=> "");

			
		function pageserver()
		{
		}
		
		function GetNewPID()
		{
			global $DB;
			$DB->query("SELECT MAX(PageID) as MAXPID FROM pages");
			$row = $DB->fetch_row();
			
			return $row['MAXPID'] + 1;
		}

		function GetContent($PID)
		{
			global $DB;
			$DB->query("SELECT * FROM pages p JOIN page_content pc ON p.PageID = pc.PageID JOIN ministries m ON p.MinistryID = m.MinistryID WHERE p.PageID = '" . $PID . "'");
			$row = $DB->fetch_row();
			
			$page = array();
			$page['title'] = $row['PageTitle'];
			$page['ministry'] = $row['MinistryName'];
			$page['content'] = $row['Content'];
			$page['advanced'] = $row['advanced'];
			$page['display_menu'] = $row['DisplayNavigation'];
			
			return $page;
		}
		
		function GetMinistryByUser($username)
		{
			global $DB;
			$DB->query("SELECT M.MinistryID , M.MinistryName,  MM.username  FROM ministries AS M, ministry_members AS MM WHERE MM.username = '" . $username . "' AND M.MinistryID = MM.MinistryID");
			
			$Ministries = array();
			while($row = $DB->fetch_row())
			{
				$Ministries[$row['MinistryID']] = $row['MinistryName'];
			}
			
			return $Ministries;			
		}

		function GetMinistryByPID($PID)
		{
			global $DB;
			$DB->query("SELECT MinistryID FROM pages WHERE PageID = '" . $PID. "'");
			
			$row = $DB->fetch_row();

			return $row['MinistryID'];			
		}
		
		function GetPagesByMinistryID($ministry_id)
		{
			global $DB;
			$DB->query("SELECT * FROM pages WHERE MinistryID='" . $ministry_id . "' ORDER BY PageTitle");
			
			while($row = $DB->fetch_row())
			{
				$this->PageGroup['pages'][$row['PageID']] = $row['PageTitle'];
			}
			
			$this->PageGroup['ministry_id'] = $ministry_id;
			
			return $this->PageGroup;			
		}
	}	
?>
